package chainSimplification.vo;

import java.nio.FloatBuffer;
import java.util.HashMap;
import java.util.Vector;

import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Line;
import com.jme.scene.Line.Mode;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jme.util.geom.BufferUtils;

import dviz.tools.BaseAniObject;
import dviz.visualSystem.impl.AnimationObject;

public class LineVO extends BaseAniObject {

	Vector<VertexVO> points;
	Line line;

	public void addPoint(HashMap<String, Object> initVector) {
		VertexVO point = (VertexVO) initVector.get("point");
		getRenderNode().attachChild(point.getRenderNode());
		points.add(point);
	}

	@Override
	public void renderUpdate(float fpt) {
		super.renderUpdate(fpt);

		Vector3f[] v3f = new Vector3f[points.size()];
		for (int i = 0; i < v3f.length; i++) {
			VertexVO v3 = points.get(i);
			v3f[i] = new Vector3f(v3.x, v3.y, v3.z);
		}
		FloatBuffer fb = BufferUtils.createFloatBuffer(v3f);
		line.reconstruct(fb, null, null, null);
		line.setCullHint(CullHint.Never);

		line.setModelBound(new BoundingSphere(10000, new Vector3f()));
		if (color == null || color.equalsIgnoreCase("white")) {
			line.setSolidColor(new ColorRGBA(1, 1, 1, 1));
		} else if (color.equalsIgnoreCase("blue")) {
			line.setSolidColor(new ColorRGBA(0, 0, 1, 1));
		} else if (color.equalsIgnoreCase("red")) {
			line.setSolidColor(new ColorRGBA(1, 0, 0, 1));
		} else {
			line.setSolidColor(new ColorRGBA(0, 1, 0, 1));
		}
		// line.setVertexCount(v3f.length);
		// getRenderNode().updateGeometricState(0, true);
		getRenderNode().updateModelBound();
		getRenderNode().updateWorldBound();
	}

	String color = "white";

	public void setColor(String color) {
		this.color = color;
	}

	public void removePoint(HashMap<String, Object> initVector) {
		VertexVO point = (VertexVO) initVector.get("point");
		getRenderNode().detachChild(point.getRenderNode());
		points.remove(point);
	}

	public LineVO(HashMap<String, Object> initVector) {
		super(initVector);
		points = new Vector<VertexVO>();
		line = new Line("", new Vector3f[0], null, null, null);

		line.setLightCombineMode(LightCombineMode.Off);
		line.setMode(Mode.Connected);
		LightState ls = DisplaySystem.getDisplaySystem().getRenderer()
				.createLightState();
		ls.setEnabled(false);
		line.setRenderState(ls);
		line.updateRenderState();
		getRenderNode().attachChild(line);

	}
}
